package com.dianqiu.common.web.action;


import java.util.ArrayList;
import java.sql.Timestamp;
import java.util.List;
import com.dianqiu.base.action.BaseAction;
import com.dianqiu.common.bean.DqBetUserMatchIdTO;
import com.dianqiu.common.bean.DqBetCompanyTO;
import com.dianqiu.common.bean.DqBetUserMatchTO;
import com.dianqiu.common.bean.DqClubsTO;
import com.dianqiu.common.bean.DqHandicapTO;
import com.dianqiu.common.bean.DqMatchPoolTO;
import com.dianqiu.common.bean.DqMatchTO;

public class BetAction extends BaseAction {

	public String bet(){
		
//		TODO
//		LoginUser loginUser = (LoginUser) this.getSession().get("loginUser");
//		Integer userId = Integer.parseInt(loginUser.getUserId());
		String userId = "1";
		
		//list all the recommend match from DqMatchPoolTO 
		List<DqMatchTO> recommendMatchList = new ArrayList<DqMatchTO>();
		List<DqMatchPoolTO> matchPoolList = this.getBetService().getAllMatchPool();
		if(matchPoolList!=null && matchPoolList.size()>0){
			for(int i=0; i<matchPoolList.size(); i++){
				DqMatchTO dqMatchTO = matchPoolList.get(i).getDqMatch();
				setMatchDateAndTime(dqMatchTO);
				recommendMatchList.add(dqMatchTO);
			}
		}
		this.getRequest().setAttribute("matchPoolList", matchPoolList);
		
		//get oldest User handicap Information of all the recommend match
		List<DqHandicapTO> recommendMatchHandicapList = this.getUserHandicapInfo(userId, recommendMatchList,"asc");
		this.getRequest().setAttribute("recommendMatchHandicapList", recommendMatchHandicapList);
		
		
		//list the matchDate from DqMatchTO
		List<DqMatchTO> matchGroupByMatchDateList = this.getBetService().getAllMatchGroupByMatchDate();
		List<String> matchDateList = new ArrayList<String>();
		if(matchGroupByMatchDateList!=null && matchGroupByMatchDateList.size()>0){
			for(int i=0; i<matchGroupByMatchDateList.size(); i++){
				DqMatchTO dqMatchTO = matchGroupByMatchDateList.get(i);
				setMatchDateAndTime(dqMatchTO);
				String matchDate = dqMatchTO.getMatchDate();
				matchDateList.add(matchDate);
			}
		}
		this.getRequest().setAttribute("matchDateList", matchDateList);
		
		
		//list all the match from DqMatchTO
		List<DqMatchTO> matchList = this.getBetService().getAllMatch();
		if(matchList!=null && matchList.size()>0){
			for(int i=0; i<matchList.size(); i++){
				DqMatchTO dqMatchTO = matchList.get(i);
				setMatchDateAndTime(dqMatchTO);
			}
		}
		this.getRequest().setAttribute("matchList", matchList);
		
		//get latest User handicap Information of all the match
		List<DqHandicapTO> handicapList = this.getUserHandicapInfo(userId, matchList,"desc");
		this.getRequest().setAttribute("handicapList", handicapList);
		
		return "success";
	}
	
	public String turnToAdd(){
		String betClbIdStr = this.getRequest().getParameter("betClbId");
		String handicapIdStr = this.getRequest().getParameter("handicapId");
		String homeOrAwayFlagStr = this.getRequest().getParameter("homeOrAwayFlag");
//		String userIdStr = this.getRequest().getParameter("userId");
		String userIdStr = "1";
		DqHandicapTO dqHandicap = null;
		DqClubsTO betClub = null;
		Integer seq = 1;
		if(betClbIdStr!=""&&handicapIdStr!=""&&betClbIdStr!=null&&handicapIdStr!=null){
			Integer betClbId = Integer.valueOf(betClbIdStr);
			Integer handicapId = Integer.valueOf(handicapIdStr);
			List<DqClubsTO> betClbList = this.getBetService().getClubesById(betClbId);
			
			if(betClbList != null){
				betClub = betClbList.get(0);
			}
		
		
			dqHandicap = this.getBetService().getHandicapById(handicapId);
			
			List<DqBetUserMatchTO> dqBetUserMatchTOList =  this.getBetService().getDqBetUserMatchListByUserIdAndHandicapIdAndMatchId(userIdStr, handicapId, dqHandicap.getDqMatch().getMthId());
			if(dqBetUserMatchTOList!=null && dqBetUserMatchTOList.size()>0){
				seq  =  dqBetUserMatchTOList.size();
			}
		}
		this.getRequest().setAttribute("betClub", betClub);
		this.getRequest().setAttribute("dqHandicap", dqHandicap);
		this.getRequest().setAttribute("homeOrAwayFlagStr", homeOrAwayFlagStr);
		this.getRequest().setAttribute("seqStr",seq);
		return "add";
	}
	
	public String add(){
		String stakeStr = this.getRequest().getParameter("stake");
//		String userIdStr = this.getRequest().getParameter("userId");
		String userIdStr = "1";
		String hndpIdStr = this.getRequest().getParameter("hndpIdStr");
		String seqStr = this.getRequest().getParameter("seqStr");
		String homeOrAwayFlagStr = this.getRequest().getParameter("homeOrAwayFlagStr");

		if(stakeStr != "" && stakeStr !=null &&
			userIdStr != "" && userIdStr !=null && 
			hndpIdStr != "" && stakeStr !=null && 
			seqStr != "" && seqStr !=null){

			Integer hndpId = Integer.valueOf(hndpIdStr);
			Integer seq = Integer.valueOf(seqStr);

			DqHandicapTO dqHandicap = this.getBetService().getHandicapById(hndpId);
			Integer mthId = dqHandicap.getDqMatch().getMthId();
			
			DqBetUserMatchIdTO betUserMatchId = new DqBetUserMatchIdTO();
			betUserMatchId.setUserId(userIdStr);
			betUserMatchId.setHndpId(hndpId);
			betUserMatchId.setSeq(seq);
			betUserMatchId.setMthId(mthId);
			
			Long stake = Long.valueOf(stakeStr);
			DqBetUserMatchTO betUserMatch = new DqBetUserMatchTO();
			
			betUserMatch.setId(betUserMatchId);
			Timestamp betDate = new Timestamp(System.currentTimeMillis());
			betUserMatch.setBetDate(betDate);
			betUserMatch.setStake(stake);
			betUserMatch.setDqHandicap(dqHandicap);
			betUserMatch.setClbBetted(Integer.valueOf(homeOrAwayFlagStr));
			betUserMatch.setResult(Long.valueOf(1));
			this.createInitialize(betUserMatch, this.getRequest());
			this.getBetService().saveDqBetUserMatchTO(betUserMatch);
			
		}
		return "save";
	}
	
	public String listBet(){
		
//		TODO
//		LoginUser loginUser = (LoginUser) this.getSession().get("loginUser");
//		Integer userId = Integer.parseInt(loginUser.getUserId());
		String userId = "1";
		
		//list all the unfinished matches
		String matchStatus = "MTHSPD";
		List<DqMatchTO> unfinishedMatchList = this.getBetService().getMatchByStatus(matchStatus);
		this.getRequest().setAttribute("unfinishedMatchList", unfinishedMatchList);
		
		//get User handicap Information of unfinished match
		List<DqHandicapTO> unfinishedMatchHandicapList = this.getUserHandicapInfo(userId, unfinishedMatchList, "desc");
		this.getRequest().setAttribute("unfinishedMatchHandicapList", unfinishedMatchHandicapList);
		
		//get User Bet Information of unfinished match
		List<DqBetUserMatchTO> unfinishedMatchUserBetList = this.getUserBetInfo(userId, unfinishedMatchList);
		this.getRequest().setAttribute("unfinishedMatchUserBetList", unfinishedMatchUserBetList);
		
		//list all the finished matches
		List<DqMatchTO> finishedMatchList = this.getBetService().getAllFinishedMatch();
		this.getRequest().setAttribute("finishedMatchList", finishedMatchList);
		
		//get User handicap Information of finished match
		List<DqHandicapTO> finishedMatchHandicapList = this.getUserHandicapInfo(userId, finishedMatchList, "desc");
		this.getRequest().setAttribute("finishedMatchHandicapList", finishedMatchHandicapList);
		
		//get User Bet Information of finished match
		List<DqBetUserMatchTO> finishedMatchUserBetList = this.getUserBetInfo(userId, finishedMatchList);
		this.getRequest().setAttribute("finishedMatchUserBetList", finishedMatchUserBetList);
		
		return "listBet";
	}
	
	public List<DqHandicapTO> getUserHandicapInfo(String userId, List<DqMatchTO> matchList, String order){
		List<DqHandicapTO> handicapList = null;
		//get preferredBetCompany according to userId
		DqBetCompanyTO preferredBetCompany = this.getBetService().getPreferredBetCompanyByUserId(userId);
		//get the latest handicap information of the company according to matchId
		if(matchList!=null && matchList.size()>0){
			for(int i=0; i<matchList.size(); i++){
				DqHandicapTO handicap = null;
				if(preferredBetCompany!=null){
					handicap = this.getBetService().getHandicapByBetCompanyIdAndMatchId(preferredBetCompany.getCmpyId(),matchList.get(i).getMthId(),order);
					handicapList.add(handicap);
				}
			}
		}
		return handicapList;
	}
	
	public List<DqBetUserMatchTO> getUserBetInfo(String userId, List<DqMatchTO> matchList){
		List<DqBetUserMatchTO> userBetMatchList = new ArrayList<DqBetUserMatchTO>();
		if(matchList!=null && matchList.size()>0){
			for(int i=0; i<matchList.size(); i++){
				DqMatchTO match = matchList.get(i);
				DqBetCompanyTO preferredBetCompany = this.getBetService().getPreferredBetCompanyByUserId(userId);
				DqHandicapTO handicap = this.getBetService().getHandicapByBetCompanyIdAndMatchId(preferredBetCompany.getCmpyId(),match.getMthId(),"desc");
				DqBetUserMatchTO betUserMatch = this.getBetService().getDqBetUserMatchByUserIdAndHandicapIdAndMatchId(userId, handicap.getHdpId(), match.getMthId());
				userBetMatchList.add(betUserMatch);
			}
		}
		return userBetMatchList;
	}
	
}
